package com.intelli4.pos.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;

import com.intelli4.pos.core.Order;
import com.intelli4.pos.core.OrderItem;
import com.intelli4.pos.core.Tax;
import com.intelli4.pos.core.TaxItem;

public class OrderPanel extends JPanel {
	private static final long serialVersionUID = -1620119572745444210L;

	private JTable itemTable;

	public OrderPanel() {
		super();

		init();
	}

	static class OrderTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 336825338701898945L;

		private Order order;

		public OrderTableModel(Order order) {
			this.order = order;
		}

		@Override
		public String getColumnName(int col) {
			switch (col) {
			case 0:
				return "Qty";
			case 1:
				return "Item";
			case 2:
				return "Unit Price";
			case 3:
				return "Price";
			}

			return null;
		}

		@Override
		public Class<?> getColumnClass(int col) {
			switch (col) {
			case 0:
				return Number.class;
			case 1:
				return String.class;
			case 2:
				return Number.class;
			case 3:
				return Number.class;
			}

			return null;
		}

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return false;
		}

		@Override
		public int getColumnCount() {
			return 4;
		}

		@Override
		public int getRowCount() {
			return order.getOrderItems().size() + order.getTaxItems().size();
		}

		@Override
		public Object getValueAt(int row, int col) {
			if (row < order.getOrderItems().size()) {
				OrderItem item = order.getOrderItems().get(row);

				switch (col) {
				case 0:
					return item.getQuantity();
				case 1:
					return item.getItemLongName();
				case 2:
					if (item.getQuantity().equals(new BigDecimal(1)))
						return null;
					else
						return item.getItemUnitPrice();
				case 3:
					return item.getItemUnitPrice().multiply(item.getQuantity(),
							MathContext.DECIMAL128);
				}

				return null;
			} else {
				TaxItem item = order.getTaxItems().get(
						row - order.getOrderItems().size());

				switch (col) {
				case 1:
					return item.getTax().getId();
				case 3:
					return item.getTaxAmount();
				}

				return null;
			}
		}
	}

	protected void init() {
		setLayout(new BorderLayout());

		Order order = new Order();
		List<OrderItem> items = new ArrayList<OrderItem>();
		OrderItem item1 = new OrderItem();
		item1.setQuantity(new BigDecimal(1));
		item1.setItemLongName("Taco");
		item1.setItemUnitPrice(new BigDecimal("1.00"));
		items.add(item1);
		OrderItem item2 = new OrderItem();
		item2.setQuantity(new BigDecimal(1));
		item2.setItemLongName("20oz Pepsi");
		item2.setItemUnitPrice(new BigDecimal("1.50"));
		items.add(item2);
		order.setOrderItems(items);

		List<TaxItem> taxItems = new ArrayList<TaxItem>();
		Tax gst = new Tax();
		gst.setId("GST");
		TaxItem tax1 = new TaxItem();
		tax1.setTax(gst);
		tax1.setTaxAmount(new BigDecimal("0.12"));
		taxItems.add(tax1);
		order.setTaxItems(taxItems);

		itemTable = new JTable(new OrderTableModel(order));
		Font font = new Font(itemTable.getFont().getName(), Font.PLAIN, 24);
		itemTable.setFont(font);
		itemTable.setRowHeight(30);
		itemTable.setIntercellSpacing(new Dimension(10, 3));

		TableColumn col0 = itemTable.getColumnModel().getColumn(0);
		col0.setPreferredWidth(100);
		TableColumn col1 = itemTable.getColumnModel().getColumn(1);
		col1.setPreferredWidth(200);
		TableColumn col2 = itemTable.getColumnModel().getColumn(2);
		col2.setPreferredWidth(100);
		TableColumn col3 = itemTable.getColumnModel().getColumn(3);
		col3.setPreferredWidth(100);

		add(new JScrollPane(itemTable), BorderLayout.CENTER);

		// JPanel summaryPanel = new JPanel(new );
	}

	public static void main(String[] args) {
		JFrame frame = new JFrame(NumberPadPanel.class.getName());
		OrderPanel panel = new OrderPanel();
		frame.add(panel, BorderLayout.CENTER);
		frame.setSize(400, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
}
